import "./index.css";
import { useNavigate } from "react-router-dom";
import { useState } from "react";
import { login } from "@/utils/back/index";
import { Input, notification } from "antd";

function Login() {
  // 管理员登录
  const navigate = useNavigate();
  const [username, setUsername] = useState("admin");
  const [password, setPassword] = useState("admin");
  const [api, contextHolder] = notification.useNotification();

  const openNotification = () => () => {
    api.open({
      message: "Notification",
      duration: 1.5,
      description: "用户名或密码错误！",
    });
  };
  const goLogin = () => {
    login({
      account: username,
      password: password,
    })
      .then((res: any) => {
        console.log(res);
        if (res.status === 401) {
          openNotification()();
        } else if (res.status === 200) {
          localStorage.setItem("token", res.data.token);
          navigate("/manage");
        }
      })
      .catch((err) => {
        console.log(err);
      });
  };
  // 游客登录
  const visitorLogin = () => {
    navigate("/manage");
  };

  return (
    <>
      {contextHolder}
      <div className="login">
        <div className="card">
          <div className="card2">
            <div className="form">
              <p id="heading">管理员登录</p>
              <div className="field">
                <svg
                  viewBox="0 0 16 16"
                  fill="currentColor"
                  height="16"
                  width="16"
                  xmlns="http://www.w3.org/2000/svg"
                  className="input-icon"
                >
                  <path d="M13.106 7.222c0-2.967-2.249-5.032-5.482-5.032-3.35 0-5.646 2.318-5.646 5.702 0 3.493 2.235 5.708 5.762 5.708.862 0 1.689-.123 2.304-.335v-.862c-.43.199-1.354.328-2.29.328-2.926 0-4.813-1.88-4.813-4.798 0-2.844 1.921-4.881 4.594-4.881 2.735 0 4.608 1.688 4.608 4.156 0 1.682-.554 2.769-1.416 2.769-.492 0-.772-.28-.772-.76V5.206H8.923v.834h-.11c-.266-.595-.881-.964-1.6-.964-1.4 0-2.378 1.162-2.378 2.823 0 1.737.957 2.906 2.379 2.906.8 0 1.415-.39 1.709-1.087h.11c.081.67.703 1.148 1.503 1.148 1.572 0 2.57-1.415 2.57-3.643zm-7.177.704c0-1.197.54-1.907 1.456-1.907.93 0 1.524.738 1.524 1.907S8.308 9.84 7.371 9.84c-.895 0-1.442-.725-1.442-1.914z"></path>
                </svg>
                <Input
                  className="input-field"
                  value={username}
                  onChange={(e) => {
                    setUsername(e.target.value);
                  }}
                />
              </div>
              <div className="field">
                <svg
                  viewBox="0 0 16 16"
                  fill="currentColor"
                  height="16"
                  width="16"
                  xmlns="http://www.w3.org/2000/svg"
                  className="input-icon"
                >
                  <path d="M8 1a2 2 0 0 1 2 2v4H6V3a2 2 0 0 1 2-2zm3 6V3a3 3 0 0 0-6 0v4a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2z"></path>
                </svg>
                <Input
                  className="input-field"
                  value={password}
                  type="password"
                  onChange={(e) => {
                    setPassword(e.target.value);
                  }}
                />
              </div>
              <div
                style={{
                  color: "#ffffff",
                  display: "flex",
                  flexDirection: "column",
                }}
              >
                <span
                  style={{
                    alignSelf: "flex-end",
                    fontSize: "13px",
                    cursor: "pointer",
                  }}
                  onClick={() => visitorLogin()}
                >
                  游客登录
                </span>
              </div>
              <div className="btn">
                <button className="button1" onClick={() => goLogin()}>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </>
  );
}

export default Login;
